package org.monster.context.core.dispatcher;

import org.monster.context.core.ApplicationFile;
import org.monster.context.core.Constants;
import org.monster.context.core.FilterConfigCache;
import org.monster.context.core.filter.ApplicationFilterChain;
import org.monster.context.core.parameter.HttpResponse;
import org.monster.context.core.request.HttpRequest;
import org.monster.context.core.response.ResponseMime;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 应用转发器
 *
 * @author Monster
 */
public class ApplicationDispatcher implements RequestDispatcher {

    /**
     * 过滤器配置键值
     */
    private final FilterConfigCache filterConfigCache = FilterConfigCache.getFilterConfigCache();
    /**
     * 响应多媒体互联网格式
     */
    private ResponseMime mime;
    /**
     * 转发URI
     */
    private String forwardURI;
    /**
     * 请求
     */
    private HttpRequest request;
    /**
     * 响应
     */
    private HttpResponse response;

    /**
     * 构造方法
     *
     * @param forwardURI 转发URI
     * @param request    请求
     * @param response   响应
     */
    public ApplicationDispatcher(String forwardURI, HttpRequest request,
                                 HttpResponse response) {
        this.forwardURI = forwardURI;
        this.request = request;
        this.response = response;
        this.mime = new ResponseMime();
    }

    /**
     * 转发
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException 服务者异常
     * @throws IOException      输入输出异常
     */
    @Override
    public void forward(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        String absolutePath = null;
        String contentType = null;
        if (forwardURI.startsWith("/")) {
            absolutePath = ApplicationFile.getAppContextPath(this.request.getContextPath())
                    + File.separator + Constants.WEB_CONTENT
                    + forwardURI.replace("/", "\\");
            contentType = mime.findMime(forwardURI);
        } else {
            String requestURI = (this.request.getContextPath() + File.separator + forwardURI)
                    .replace(File.separator, "/");
            filterConfigCache.setForwardURI("/" + requestURI);
            this.request.setURI(this.request.getMethod() + " " + requestURI
                    + " " + this.request.getProtocol());
            ApplicationFilterDispatcher filterDispatcher = new ApplicationFilterDispatcher(
                    this.request, this.response);
            filterDispatcher.doFilter(this.request, this.response,
                    new ApplicationFilterChain());
        }

        if (contentType != null) {
            HttpResponse httpResponse = new HttpResponse(response.getOutputStream());
            httpResponse.setProtocol(request.getProtocol());
            httpResponse.setContentType(contentType);
            httpResponse.getWriter();
            httpResponse.sendResource(new File(absolutePath));
        }
    }

    /**
     * 包含
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException 服务者异常
     * @throws IOException      输入输出异常
     */
    @Override
    public void include(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {

    }

}
